package com.miracle9.game.service;

import com.miracle9.game.dao.UserTopServiceDao;
import com.miracle9.game.entity.User;
import com.miracle9.game.entity.UserTop;
import com.miracle9.game.entity.UserTop2;
import com.miracle9.game.enums.GameTypeEnums;
import com.miracle9.game.util.FangYiUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userTopService")
public class UserTopService {

    @Autowired
    private UserTopServiceDao userDao;
    private Logger logger = Logger.getLogger(UserTopService.class);

    public Map<String, Object> topList(int gameType, int topListType, IoSession session) {
        Map result = new HashMap();
        result.put("gameType", Integer.valueOf(gameType));
        result.put("topListType", Integer.valueOf(topListType));
        List tops = this.userDao.topList(gameType - 1, topListType);
        result.put("userTop", tops.toArray(new UserTop[0]));
        return result;
    }

    public List<UserTop> topList(int nGameType, int nTopListType) {
        return this.userDao.topList(nGameType, nTopListType);
    }

    public Map<String, Object> getRankList(int type, int topType, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        result.put("type", type);
        result.put("success", Boolean.TRUE);
        //财富榜、等级榜
        if (type == 1 || type == 2) {
            List<User> tops = this.userDao.topList2(type);
            List<UserTop2> rankList = new ArrayList<>();
            for (User u : tops) {
                UserTop2 ut = new UserTop2();
                ut.setId(u.getId());
                ut.setGameGoldOrLevel(type == 1 ? u.getGameGold() : u.getLevel());
                ut.setNickname(u.getNickname());
                ut.setPhotoId(u.getPhotoId());
                rankList.add(ut);
            }
            result.put("rankList", rankList);
        } else if (type == 33) {
            //李逵
            List<UserTop> tops = this.userDao.topList(GameTypeEnums.GT_20.getType(), topType);
            if(CollectionUtils.isNotEmpty(tops)){
                for (UserTop userTop:tops) {
                    if(StringUtils.isNotBlank(userTop.getAwardName())){
                        userTop.setAwardName(FangYiUtil.fangyi(userTop.getAwardName()));
                    }
                }
            }
            result.put("rankList", tops);
        } else if (type == 34) {
            //金蟾
            List<UserTop> tops = this.userDao.topList(GameTypeEnums.GT_37.getType(), topType);
            if(CollectionUtils.isNotEmpty(tops)){
                for (UserTop userTop:tops) {
                    if(StringUtils.isNotBlank(userTop.getAwardName())){
                        userTop.setAwardName(FangYiUtil.fangyi(userTop.getAwardName()));
                    }
                }
            }
            result.put("rankList", tops);
        }  else if (type == 23) {
            //摇钱树
            List<UserTop> tops = this.userDao.topList(GameTypeEnums.GT_1.getType(), topType);
            if(CollectionUtils.isNotEmpty(tops)){
                for (UserTop userTop:tops) {
                    if(StringUtils.isNotBlank(userTop.getAwardName())){
                        userTop.setAwardName(FangYiUtil.fangyi(userTop.getAwardName()));
                    }
                }
            }
            result.put("rankList", tops);
        } else {
            List<UserTop> tops = this.userDao.topList(type - 3, topType);
            if(CollectionUtils.isNotEmpty(tops)){
                for (UserTop userTop:tops) {
                    if(StringUtils.isNotBlank(userTop.getAwardName())){
                        userTop.setAwardName(FangYiUtil.fangyi(userTop.getAwardName()));
                    }
                }
            }
            result.put("rankList", tops);
        }
        return result;
    }

    public static String getExceptionAllinformation(Exception ex) {
        String sOut = "";
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut = sOut + "\tat " + s + "\r\n";
        }
        return sOut;
    }

}
